package com.im.controller;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.im.service.common.smo.ICommonSMO;
import com.im.service.dto.FuncMenu;
import com.im.service.dto.Staff;
import com.im.service.user.smo.IUserOperSMO;
import com.im.service.user.smo.IUserQuerySMO;
import com.im.util.JsonUtil;
import com.im.util.ValidateCode;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("loginController")
public class LoginController  extends BaseController{

    @Autowired
    private IUserQuerySMO userQuerySMO;
    @Autowired
    private IUserOperSMO userOperSMO;
    @Autowired
    private ICommonSMO commonSMO;

    @RequestMapping("/login")
    public String blacklist(Model model){
        super.putMDA(model);
        return "login/login";
    }
    @RequestMapping("/noPermisssion")
    public String noPermisssion(Model model){
        super.putMDA(model);
        return "error/NoPermission";
    }

    @RequestMapping("/getValidCode")
    public void  getValidCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("image/jpeg");
        // 禁止图像缓存。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);

        HttpSession session = request.getSession();

        ValidateCode vCode = new ValidateCode(155, 44, 4, 10);
        session.setAttribute("ValidateCode", vCode.getCode().toUpperCase());
        vCode.write(response.getOutputStream());
    }

    @RequestMapping("/validate")
    @ResponseBody
    public Object  validate(HttpServletRequest request,@RequestBody String jsonString) throws IOException {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("resultCode", "0");
        resultMap.put("resultMsg", "登陆成功！");
        //校验验证码
        HttpSession session = request.getSession();
        Map param = JsonUtil.getMap(jsonString);
        Object validCode = param.get("validCode");
        if (validCode == null || "".equals(validCode) || !validCode.toString().toUpperCase().equals(session.getAttribute("ValidateCode"))) {
            resultMap.put("resultCode", "-1");
            resultMap.put("resultMsg", "验证码错误。");
            return JSONObject.fromObject(resultMap).toString();
        }
        //登录时,使用staffCode+pwd
        Staff staff = userOperSMO.login(param);
        //String acctNum = request.getParameter("name");
        //String acctPasswd = request.getParameter("password");


        boolean loginSuccess = true;

        if(staff == null){
            resultMap.put("resultCode", "-1");
            resultMap.put("resultMsg", "账号或密码不正确!");
            loginSuccess = false;
        }else{
            //登录成功、设置session
            session.setAttribute("loginedStaff",staff);
            //设置seesionId
            commonSMO.putValueToRedis("SEESION_ID_"+staff.getStaffId(),session.getId(),2*3600*1000L);
        }

        return   JSONObject.fromObject(resultMap).toString();
    }


}
